
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { notification } from 'ant-design-vue'

export const usePointStore = defineStore('point', () => {
  const showNotification = ref(false)
  const notificationDescription = ref('')
  const lastAddedPoints = ref(0)
  const selectedStudentsForPoints = ref([])
  let addPointsTimeout = null

  const addPoints = async (points, isRight = 1, student) => {
    if (!student) return
    lastAddedPoints.value = points
    notificationDescription.value = points <= 0
      ? `学生 ${student.stu_name}继续努力`
      : `学生 ${student.stu_name} 增加了 ${points} 分`
    showNotification.value = true
    addPointsTimeout = setTimeout(async () => {
      const response = await window.electronAPI.addPoints({
        stu_no: student.stu_no,
        points,
        isRight,
      })
      if (response.success) {
        lastAddedPoints.value = 0
        showNotification.value = false
      } else {
        notification.error({
          message: '加分失败',
          description: response.message || '未知错误',
          duration: 3,
        })
        lastAddedPoints.value = 0
        showNotification.value = false
      }
    }, 5000)
  }

  const addCustomPoints = async (customPoints, student) => {
    if (!student || customPoints <= 0) return
    lastAddedPoints.value = customPoints
    notificationDescription.value = `学生 ${student.stu_name} 增加了 ${customPoints} 分`
    showNotification.value = true
    addPointsTimeout = setTimeout(async () => {
      const response = await window.electronAPI.addPoints({
        stu_no: student.stu_no,
        points: customPoints,
        isRight: 1,
      })
      if (response.success) {
        lastAddedPoints.value = 0
        showNotification.value = false
      } else {
        notification.error({
          message: '加分失败',
          description: response.message || '未知错误',
          duration: 3,
        })
        lastAddedPoints.value = 0
        showNotification.value = false
      }
    }, 5000)
  }

  const undoPoints = () => {
    if (addPointsTimeout) {
      clearTimeout(addPointsTimeout)
      addPointsTimeout = null
      notificationDescription.value = ''
      lastAddedPoints.value = 0
      showNotification.value = false
      notification.destroy()
      notification.success({
        message: '撤销成功',
        description: '加分操作已撤销',
        duration: 3,
      })
    }
  }

  const addPointsToSelected = async (points, isRight = 1) => {
    if (selectedStudentsForPoints.value.length === 0) return
    const data = selectedStudentsForPoints.value.map(student => ({
      stu_no: student.stu_no,
      points,
      isRight,
    }))
    notificationDescription.value = points <= 0
      ? `以下学生继续努力：${selectedStudentsForPoints.value.map(s => s.stu_name).join(', ')}`
      : `为以下学生加了 ${points} 分：${selectedStudentsForPoints.value.map(s => s.stu_name).join(', ')}`
    showNotification.value = true
    addPointsTimeout = setTimeout(async () => {
      const response = await window.electronAPI.addPoints(data)
      if (response.success) {
        showNotification.value = false
      } else {
        notification.error({
          message: '加分失败',
          description: response.message || '未知错误',
          duration: 3,
        })
      }
    }, 5000)
  }

  const addCustomPointsToSelected = async (customPoints) => {
    if (selectedStudentsForPoints.value.length === 0 || customPoints <= 0) return
    const data = selectedStudentsForPoints.value.map(student => ({
      stu_no: student.stu_no,
      points: customPoints,
      isRight: 1,
    }))
    notificationDescription.value = `为以下学生加了 ${customPoints} 分：${selectedStudentsForPoints.value.map(s => s.stu_name).join(', ')}`
    showNotification.value = true
    addPointsTimeout = setTimeout(async () => {
      const response = await window.electronAPI.addPoints(data)
      if (response.success) {
        showNotification.value = false
      } else {
        notification.error({
          message: '加分失败',
          description: response.message || '未知错误',
          duration: 3,
        })
      }
    }, 5000)
  }
  const toggleStudentSelection = (student) => {
    if (isStudentSelected(student)) {
      selectedStudentsForPoints.value = selectedStudentsForPoints.value.filter(s => s.stu_no != student.stu_no)
    } else {
      selectedStudentsForPoints.value.push(student)
    }
  }
  const setSelectedStudents = (students) => {
    selectedStudentsForPoints.value = students
  }
  const isStudentSelected = (student) => {
    return selectedStudentsForPoints.value.some(s => s.stu_no == student.stu_no)
  }
  const deselectAllStudents = () => {
    selectedStudentsForPoints.value = []
  }

  const selectAllStudents = (students) => {
    selectedStudentsForPoints.value = students
  }

  return {
    showNotification,
    notificationDescription,
    lastAddedPoints,
    selectedStudentsForPoints,
    isStudentSelected,
    addPoints,
    addCustomPoints,
    undoPoints,
    addPointsToSelected,
    addCustomPointsToSelected,
    setSelectedStudents,
    deselectAllStudents,
    selectAllStudents,
    toggleStudentSelection
  }
})
